﻿namespace K9Nano.Session;

public interface IUserContext
{
    long UserId { get; }

    string Username { get; }

    string Name { get; }

    long? OrganizationId { get; }

    /// <summary>
    /// 角色列表
    /// </summary>
    HashSet<string> Roles { get; set; }

    /// <summary>
    /// 权限列表
    /// </summary>
    HashSet<string> Permissions { get; set; }

    /// <summary>
    /// 是否拥有指定角色
    /// </summary>
    bool HasRole(string role) => Roles.Contains(GlobalOptions.RoleNames.Admin, StringComparer.Ordinal)
            || Roles.Contains(role, StringComparer.Ordinal);

    /// <summary>
    /// 是否拥有指定权限
    /// </summary>
    bool HasPermission(string permission) => Roles.Contains(GlobalOptions.RoleNames.Admin, StringComparer.Ordinal)
            || Permissions.Contains(permission, StringComparer.Ordinal);

    /// <summary>
    /// 是否拥有其中一个权限
    /// </summary>
    bool HasAnyPermission(params string[] permissions) => Roles.Contains(GlobalOptions.RoleNames.Admin, StringComparer.Ordinal)
            || permissions.Any(a => Permissions.Contains(a, StringComparer.Ordinal));

    /// <summary>
    /// 是否拥有其中一个角色
    /// </summary>
    bool HasAnyRole(params string[] roles) => Roles.Contains(GlobalOptions.RoleNames.Admin, StringComparer.Ordinal)
            || roles.Any(a => Roles.Contains(a, StringComparer.Ordinal));
}